package com.liuzeyuan.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.liuzeyuan.entity.User;
import com.liuzeyuan.entity.Api;
import com.liuzeyuan.service.UserService;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@RequestMapping("/user")
@Controller
@Slf4j

public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return "index";
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    public String importExcel(MultipartFile excelFile) throws Exception {
        log.info("文件名:[{}]", excelFile.getOriginalFilename());
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        List<User> users = ExcelImportUtil.importExcel(excelFile.getInputStream(), User.class, params);
        userService.savaAll(users);
        users.forEach(System.out::println);
        return "redirect:/user/findAll";
    }

    /**
     * 导出excel
     */
    @RequestMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<User> users = userService.findAll();
        //生成excel
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户列表信息", "用户信息"), User.class, users);
        response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("用户列表.xls", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }


    /**
     * 导出api请求的data
     */
    @RequestMapping("/export/api")
    public void exportExcelVue(HttpServletResponse response) throws IOException {
        Map<String, Object> map = MapHelper.map(
                "pid", "75716633",
                "branchId", "ZUqEdvA4",
                "sid", "78237135");
        String responseData = HttpUtil.post("https://easydoc.net/api/v2/share/children", map);
        JSONObject jsonObject = JSONUtil.parseObj(responseData);
        Api api = new Api();
        api.setId(jsonObject.getStr("_id"))
                .setName(jsonObject.getStr("name"))
                .setVersion(jsonObject.getStr("version"));
        ArrayList<Object> list = new ArrayList<>();
        list.add(api);
        //生成excel
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("api", "api"), Api.class, list);
        response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("api.xls", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
}
